package com.easy.work.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.syslib.util.AndroidUtils;
import com.base.syslib.util.MessageTools;
import com.base.syslib.view.RoundImageView;
import com.easy.work.MainActivity;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseFragment;
import com.easy.work.app.CommonValue;
import com.easy.work.function.SettingActivity;
import com.easy.work.function.me.CashDrawActivity;
import com.easy.work.function.me.CashDrawListAct;
import com.easy.work.function.me.InvitationCodeAct;
import com.easy.work.function.me.MyCollectAct;
import com.easy.work.function.me.MyWorkAct;
import com.easy.work.function.me.MyWorkListActivity;
import com.easy.work.function.resume.MyResumeAct;
import com.easy.work.http.response.ResumeInfoResponse;
import com.easy.work.utils.SP.SPKey;
import com.easy.work.utils.StringUtil;
import com.easy.work.utils.UserOssUtils;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Admin on 2017/12/6.
 */

public class MeFragment extends BaseFragment {
    @BindView(R.id.img_person)
    RoundImageView imgPerson;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ly_my_work)
    LinearLayout lyMyWork;
    @BindView(R.id.ly_my_collection)
    LinearLayout lyMyCollection;
    @BindView(R.id.ly_my_invitation_code)
    LinearLayout lyMyInvitationCode;
    //    @BindView(R.id.tv_cash_withdrawal)
//    TextView tvCashWithdrawal;
    @BindView(R.id.rl_cash_withdrawal)
    RelativeLayout rlCashWithdrawal;
    @BindView(R.id.ly_about_us)
    LinearLayout lyAboutUs;
    @BindView(R.id.rl_online_service)
    RelativeLayout rlOnlineService;
    @BindView(R.id.ly_setting)
    LinearLayout lySetting;

    @BindView(R.id.img_message_detail)
    LinearLayout imgMessageDetail;
    Unbinder unbinder;

    /**
     * 个人信息
     */
    ResumeInfoResponse resumeInfoResponse;

    @Override
    public int getFragmentLayout() {
        return R.layout.frag_me;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        String dealImgUrl = UserOssUtils.uploadUrl + CommonValue.headImage + AppSession.getInstance().getUserId() + ".png";
        Picasso.with(getContext()).load(dealImgUrl).error(R.mipmap.personal_default).into(imgPerson);
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //隐藏时所作的事情

        } else {
            //显示时所作的事情
            resumeInfoResponse = ((MainActivity) getActivity()).resumeInfoResponse;
            String name = "";
            if (resumeInfoResponse != null) {
                name = resumeInfoResponse.name;
            }

            if (StringUtil.isEmpty(name)) {
                name = AppSession.getAppSPDataNoId(SPKey.phone);
                name = name.substring(name.length() - 6, name.length());
            }
            tvName.setText(name);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ly_my_work, R.id.ly_my_collection, R.id.ly_my_invitation_code,
            R.id.rl_cash_withdrawal, R.id.ly_about_us, R.id.rl_online_service,
            R.id.ly_setting, R.id.img_message_detail, R.id.img_person, R.id.flag_ll_title_mid})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ly_my_work:
//                MyWorkAct.startActivity(getContext());
                MyWorkListActivity.start(getContext());
                break;
            case R.id.ly_my_collection:
                MyCollectAct.start(getContext());
                break;
            case R.id.ly_my_invitation_code:
                if (resumeInfoResponse != null) {
                    InvitationCodeAct.startActivity(getContext(), resumeInfoResponse.inviteCode);
                } else {
                    InvitationCodeAct.startActivity(getContext(), null);
                }

                break;
            case R.id.rl_cash_withdrawal:
                CashDrawListAct.start(getActivity());
                break;
            case R.id.ly_about_us:
                break;
            case R.id.rl_online_service:
                MessageTools.showDialog(getActivity(), "是否拨打客服电话", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        AndroidUtils.toCallView(getContext(), "051983822852");
                    }
                });
                break;
            case R.id.ly_setting:
                SettingActivity.start(getContext());
                break;

            case R.id.img_message_detail:
            case R.id.img_person:
            case R.id.flag_ll_title_mid:
                //我的简历
                MyResumeAct.startActivity(getContext());
                break;
        }
    }
}
